package com.github.codemperor.api.base;


import java.io.Serializable;

/**
 * 统一response
 *
 * @author codemperor
 */
public class BaseResponse<T> implements Serializable {

    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * code
     */
    private int code;

    /**
     * 描述信息
     */
    private String resultMessage;

    /**
     * 业务数据
     */
    private T data;

    public static BaseResponse ok() {
        return new BaseResponse(true, BaseCodeEnum.OK.getCode());
    }

    public static BaseResponse ok(Object data) {
        return new BaseResponse<>(true, BaseCodeEnum.OK.getCode(), data);
    }

    public static BaseResponse failure() {
        return new BaseResponse(false, -1);
    }

    public static BaseResponse failure(Object data) {
        return new BaseResponse<>(false, -1, data);
    }

    public BaseResponse(Boolean success, BaseCodeEnum ex) {
        this.success = success;
        this.code = ex.getCode();
        this.resultMessage = ex.getMessage();
    }

    // 返回码父级抽象接口
    public BaseResponse(Boolean success, IBaseCodeEnum ex) {
        this.success = success;
        this.code = ex.getCode();
        this.resultMessage = ex.getMessage();
    }

    public BaseResponse() {
        super();
    }

    public BaseResponse(Boolean success, int code, String message, T data) {
        this.success = success;
        this.code = code;
        this.resultMessage = message;
        this.data = data;
    }

    public BaseResponse(Boolean success, int code, T data) {
        this.success = success;
        this.code = code;
        this.data = data;
    }

    public BaseResponse(Boolean success, int code) {
        this.success = success;
        this.code = code;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getResultMessage() {
        return resultMessage;
    }

    public void setResultMessage(String resultMessage) {
        this.resultMessage = resultMessage;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"success\":")
                .append(success);
        sb.append(",\"code\":")
                .append(code);
        sb.append(",\"resultMessage\":\"")
                .append(resultMessage).append('\"');
        sb.append(",\"data\":")
                .append(data);
        sb.append('}');
        return sb.toString();
    }
}
